<template>
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <h1>登录游戏</h1>
            </div>
            <div class="login-form">
                <div v-if="userStore.error" class="login-error">{{ userStore.error }}</div>

                <div class="form-item">
                    <label>账号</label>
                    <input
                        v-model="loginForm.account"
                        :disabled="userStore.loading"
                        placeholder="请输入用户名或邮箱"
                        type="text"
                    />
                </div>
                <div class="form-item">
                    <label>密码</label>
                    <input
                        v-model="loginForm.password"
                        :disabled="userStore.loading"
                        placeholder="请输入密码"
                        type="password"
                    />
                </div>
                <div class="form-actions">
                    <button :disabled="userStore.loading" class="login-btn" @click="handleLogin">
                        {{ userStore.loading ? '登录中...' : '登 录' }}
                    </button>
                    <button
                        :disabled="userStore.loading"
                        class="register-btn"
                        @click="goToRegister"
                    >
                        注 册
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
    import { onMounted, reactive } from 'vue'
    import { useRouter } from 'vue-router'
    import { ElMessage } from 'element-plus'
    import { useUserStore } from '@/stores/user'

    const router = useRouter()
    const userStore = useUserStore()

    onMounted(() => {
        //如果jwt_token存在，就直接登录成功
        const token = localStorage.getItem('token')
        if (token) {
            userStore.updateToken(token)
            userStore.getinfo({
                success() {
                    ElMessage.success('登录成功！')
                    router.push('/')
                },
            })
        }
    })

    const loginForm = reactive({
        account: '',
        password: '',
    })

    const handleLogin = async () => {
        // 表单验证
        if (!loginForm.account) {
            ElMessage.warning('请输入用户名或邮箱')
            return
        }

        if (!loginForm.password) {
            ElMessage.warning('请输入密码')
            return
        }

        // 使用userStore的登录方法
        const success = await userStore.login(loginForm.account, loginForm.password)

        if (success) {
            ElMessage.success('登录成功！')
            await router.push('/')
        }
    }

    const goToRegister = () => {
        router.push('/register')
    }
</script>

<style scoped>
    .login-container {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        width: 100vw;
    }

    .login-card {
        width: 400px;
        background-color: rgba(0, 0, 0, 0.7);
        border-radius: 12px;
        padding: 30px;
        box-shadow: 0 0 30px rgba(255, 215, 0, 0.3);
        border: 1px solid rgba(255, 215, 0, 0.3);
        color: #fff;
        animation: fadeIn 0.5s ease-in-out;
        transform: scale(1);
        transition: transform 0.3s ease;
    }

    .login-card:hover {
        transform: scale(1.02);
        box-shadow: 0 0 40px rgba(255, 215, 0, 0.5);
    }

    .login-header {
        text-align: center;
        margin-bottom: 30px;
    }

    .login-header h1 {
        font-size: 28px;
        color: #ffd700;
        text-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
        margin: 0;
        letter-spacing: 2px;
    }

    .login-error {
        background-color: rgba(255, 87, 87, 0.2);
        color: #ff5757;
        padding: 10px;
        border-radius: 6px;
        margin-bottom: 20px;
        text-align: center;
        font-size: 14px;
        border: 1px solid rgba(255, 87, 87, 0.3);
    }

    .form-item {
        margin-bottom: 20px;
    }

    .form-item label {
        display: block;
        margin-bottom: 8px;
        font-size: 16px;
        color: #ffd700;
    }

    .form-item input {
        width: 100%;
        background-color: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(255, 215, 0, 0.3);
        border-radius: 6px;
        padding: 12px 15px;
        color: #fff;
        font-size: 16px;
        transition: all 0.3s;
        box-sizing: border-box;
    }

    .form-item input:focus {
        outline: none;
        border-color: #ffd700;
        box-shadow: 0 0 8px rgba(255, 215, 0, 0.6);
    }

    .form-item input::placeholder {
        color: rgba(255, 255, 255, 0.5);
    }

    .form-actions {
        display: flex;
        justify-content: space-between;
        margin-top: 30px;
    }

    .login-btn,
    .register-btn {
        flex: 1;
        padding: 12px 0;
        font-size: 16px;
        font-weight: bold;
        border-radius: 6px;
        cursor: pointer;
        transition: all 0.3s;
        border: none;
    }

    .login-btn {
        background-color: #ffd700;
        color: #000;
        margin-right: 10px;
    }

    .login-btn:hover {
        background-color: #ffc000;
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(255, 215, 0, 0.4);
    }

    .register-btn {
        background-color: transparent;
        color: #ffd700;
        border: 1px solid #ffd700;
    }

    .register-btn:hover {
        background-color: rgba(255, 215, 0, 0.1);
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(255, 215, 0, 0.2);
    }

    button:disabled {
        opacity: 0.7;
        cursor: not-allowed;
        transform: none !important;
        box-shadow: none !important;
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(-20px) scale(0.95);
        }
        to {
            opacity: 1;
            transform: translateY(0) scale(1);
        }
    }
</style>
